package com.mediacloud.app.newsmodule.view

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.mediacloud.app.assembly.clickutils.NoDoubleClickUtils
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.spider.FollowKit
import com.mediacloud.app.newsmodule.utils.spider.IAttentionCall
import com.mediacloud.app.user.model.UserInfo
import com.zimeiti.details.MediaAuthorDetailActivity

class BaseSpiderViewX @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), View.OnClickListener ,IAttentionCall{

    var articleItem: ArticleItem? = null
    var spiderImage: ImageView
    var icon_img: ImageView
    var spiderFollow: TextView
    var spiderFollowGroup: LinearLayout
    var spiderAuthor: TextView
    var call:IAttentionCall?=null

    init {
        View.inflate(context, R.layout.base_spider_layoutx, this)
        spiderImage = findViewById(R.id.spiderImage)
        icon_img = findViewById(R.id.icon_img)
        spiderAuthor = findViewById(R.id.spiderAuthor)
        spiderFollow = findViewById(R.id.spiderFollow)
        spiderFollowGroup = findViewById(R.id.spiderFollowGroup)
    }


    fun setDate(articleItem: ArticleItem) {
        visibility = View.VISIBLE
        this.articleItem = articleItem
        val spider = articleItem.spider
        val drawable = ContextCompat.getDrawable(context, R.drawable.new_user_logo_login)
        drawable?.let { spiderImage.load(spider?.userImage, it) }
        spiderAuthor.text = spider?.userNickName
        spiderFollowGroup.setOnClickListener(this)
        spiderAuthor.setOnClickListener(this)
        spiderImage.setOnClickListener(this)
        checkIsAttention()

    }

    fun attention() {
        FollowKit.followOrNot(null,context, articleItem?.spider?.userId, articleItem?.spider?.userNickName,this,articleItem)
       /* val operate = if (!spiderFollowGroup.isSelected) DataCollectRequestButtonClickExpansion.POSITIVE_ACTION else DataCollectRequestButtonClickExpansion.NEGATIVE_ACTION
        val userInfo = UserInfo.getUserInfo(context)
        if (context is IButtonClickDataCollect) {
            DataCollectRequestButtonClickExpansion.operateMeidaAttention(
                context.javaClass.name,
                articleItem?.spider?.userId,
                articleItem?.spider?.userNickName,
                operate,
                userInfo.userid,
                userInfo.nickname
            )
        } else {
            //页面的名字有点问题,先暂时这样处理吧
            DataCollectRequestButtonClickExpansion.operateMeidaAttention(
                this.javaClass.name,
                articleItem?.spider?.userId,
                articleItem?.spider?.userNickName,
                operate,
                userInfo.userid,
                userInfo.nickname
            )
        }*/
    }

    fun checkIsAttention() {
        if (!UserInfo.isLogin(context)) {
            spiderFollowGroup.visibility = VISIBLE
            return
        }
        articleItem?.let {
            onAttentionResult(it.isFollowed)
        }
    }

    fun goSpiderHome() {
        val author_id = articleItem?.spider?.userId
        if (!TextUtils.isEmpty(author_id)) {
            MediaAuthorDetailActivity.start(context, author_id)
        }
    }


    override fun onClick(v: View?) {
        if (v == spiderAuthor || v == spiderImage) {
            goSpiderHome()
        } else if (v == spiderFollowGroup) {
            if (NoDoubleClickUtils.scrollRepeatOut()) {
                return
            }
            attention()
        }
    }

    override fun onAttentionResult(attention: Boolean) {
        spiderFollowGroup.visibility = VISIBLE
        spiderFollowGroup.isClickable = true
        if (attention) {
            spiderFollowGroup.isSelected = true
            spiderFollow.text = "已关注"
            spiderFollow.setTextColor(ContextCompat.getColor(context,R.color.white))
            icon_img.setImageResource(R.drawable.icon_atted)
        } else {
            spiderFollowGroup.isSelected = false
            spiderFollow.text = "关注"
            spiderFollow.setTextColor(ContextCompat.getColor(context,R.color.cctv7_red))
            icon_img.setImageResource(R.drawable.icon_att)
        }
        call?.onAttentionResult(attention)
    }

    override fun onAttentionFail() {
        spiderFollowGroup.isClickable = true
        call?.onAttentionFail()
    }

}